﻿import os
import random
import time

import locals
from utils.functions import *
from utils.rectangle import Rectangle
from utils.vector import Vector, vector_spawn

"""
对于classes中的类，只涉及数据处理
有关于输出的以对象形式交给main处理
"""


class Event:
    # 事件类
    # 通过传导对象来调用main中的函数
    def __init__(self, event, args):
        self.event = event
        self.args = args

class Entity:
    # 基础的实体类，提供通用功能
    def __init__(self, name, x, y):
        self.name = name
        self.x = x
        self.y = y
        self.velocity = Vector(0, 0)
        self.id = None
        self.outputEventArray = []
        self.info = []
        self.be_selected = False

    def self_event(self, enviro):
        self.outputEventArray = []
        entity, now_keys, now_mouse, controlHost = enviro
        # 新置一个ID
        if self.id is None:
            ran_id = random.randint(0, 99999)
            id_array = []
            for each_entity in entity:
                if each_entity.id is not None:
                    id_array.append(each_entity.id)
            while ran_id in id_array:
                ran_id = random.randint(0, 99999)
            self.id = ran_id
        # 读取数据
        if self.info == [] or self.info is None:
            self.reload_info()

    def reload_info(self):
        # 读取文件的内容
        way = "bin/data/" + self.name + ".dat"
        self.info = read_data(way, "|")

    def load_info(self, arg_name):
        # 读取单位的数据
        for arg_kind in self.info:
            if arg_kind[0] == arg_name:
                if is_number(arg_kind[1]):
                    return float(arg_kind[1])
                else:
                    return arg_kind[1]

class Camera(Entity):
    # 继承Entity，为玩家提供绘制参考系
    # 不直接参与作画
    def __init__(self, name, x, y, showForID):
        super().__init__(name, x, y)
        self.showForID = showForID
        self.screenMutt = 1

    def self_event(self, enviro):
        super().self_event(enviro)
